$().ready(function() {
	DriverFilter.init();
	
});

var DriverFilter = {
		_allDrivers : new Array(),
		_filteredDrivers : new Array(),
		// Filter parametres
		carsArray : [],
		km_costArray : { from: '', to: ''},
		lastExpirienceArray : [],
		distanceWishesArray : {from: '', to: ''},
		
		init : function() {
			var self = this;
			self._filteredDrivers = self._allDrivers;
			
			$(".driver-line").each(function() {
				self._allDrivers.push(new Driver($(this).data("info")));
			});
			
			$('.top-filter .drop-filter-block input:checkbox, .top-filter input:text').live('change', function(e) {
				var srcElement = e.srcElement ? e.srcElement.name : e.target.name;
				self.changeFilterParametres(srcElement);
			});
		},
		
		changeFilterParametres : function(filterType) {
			switch(filterType) {
				case 'Cars' :
					this.changeCarParametres();
					break;
				case 'km_cost' :
					this.changeKmCostParametres();
					break;
				case 'last_expirience' :
					this.changeExpirienceParametres();
					break;
				case 'distance_wishes' :
					this.changeDistanceWishesParametres();
					break;
					
			}

			this.makeFullFilter();
			
			$(".driver-info .information").text('');
			$(".driver-info").removeAttr('id-driver');
		},
		
		changeCarParametres : function() {
			var self = this;
			this.carsArray = [];
			$(".car-type :checkbox[name=Cars]").each(function() {
				if($(this).is(":checked")) self.carsArray.push($(this).val());
			});
		},
		changeKmCostParametres : function() {
			var kmCostFrom = $('.km_cost #from').val();
			var kmCostTo = $('.km_cost #to').val();
			var regExp = /^[1-9]{0,1}[0-9]{0,3}$/;
			
			if (regExp.test(kmCostFrom)) this.km_costArray.from = kmCostFrom;
			else {
				this.km_costArray.from = '';
				$('.km_cost #from').val('');
			}
			
			if (regExp.test(kmCostTo)) this.km_costArray.to = kmCostTo;
			else {
				this.km_costArray.to = '';
				$('.km_cost #to').val('');
			}
			if(parseInt(this.km_costArray.to) < parseInt(this.km_costArray.from)) {
				this.km_costArray.to = this.km_costArray.from;
				$('.km_cost #to').val(kmCostFrom);
			}

		},
		changeExpirienceParametres : function() {
			var self = this;
			this.lastExpirienceArray = [];
			$(".expirience :checkbox[name=last_expirience]").each(function() {
				if($(this).is(":checked")) self.lastExpirienceArray.push($(this).val());
			});
		},
		changeDistanceWishesParametres : function() {
			var distanceFrom = $('.distance_wishes  #from').val();
			var distanceTo = $('.distance_wishes #to').val();
			var regExp = /^[0-9]{0,5}$/;
			
			if (regExp.test(distanceFrom)) this.distanceWishesArray.from = distanceFrom;
			else {
				this.distanceWishesArray.from = '';
				$('.distance_wishes  #from').val('');
			}
			
			if (regExp.test(distanceTo)) this.distanceWishesArray.to = distanceTo;
			else {
				this.distanceWishesArray.to = '';
				$('.distance_wishes  #to').val('');
			}
			if(parseInt(this.distanceWishesArray.to) < parseInt(this.distanceWishesArray.from)) {
				this.distanceWishesArray.to = this.distanceWishesArray.from;
				$('.distance_wishes  #to').val(distanceFrom);
			}
			
		},
		
		
		makeFullFilter : function() {
			var self = this;
			var checkedSuccess;
			var filteredDrivers = [];
			var currentDriver;
			
			for(var i=0; i < this._allDrivers.length; i++) {
				currentDriver = this._allDrivers[i];
				checkingFailed = false;
				
				// car filter
				if(this.carsArray.length != 0) {
					var k = false;
					for (var j=0; j < currentDriver.carsArray[0].length; j++) {
						if ($.inArray(currentDriver.carsArray[0][j].carrying_capacity, self.carsArray) + 1) k=true;
					}
					if(!k) checkingFailed = true;
				}
				
				// km cost filter
				if(!checkingFailed) {
					if(this.km_costArray.from != '' &&  this.km_costArray.to != '') {
						if(currentDriver.kmCost.from > parseInt(this.km_costArray.to) || currentDriver.kmCost.to < parseInt(this.km_costArray.from)) {
							checkingFailed = true;
						}
					}
					else if(this.km_costArray.from != '' &&  this.km_costArray.to == '') {
						if(currentDriver.kmCost.to < parseInt(this.km_costArray.from)) {						
							checkingFailed = true;
						}
					}
					else if(this.km_costArray.from == '' &&  this.km_costArray.to != '') {
						console.log(currentDriver.kmCost.from);
						console.log(parseInt(this.km_costArray.to));
						if(currentDriver.kmCost.from > parseInt(this.km_costArray.to)) {						
							checkingFailed = true;
						}
					}
					
				}
				
				// expirience filter
				if(!checkingFailed) {
					if(this.lastExpirienceArray.length != 0)  {
						if (!($.inArray(currentDriver.lastExpirience, self.lastExpirienceArray) + 1)) checkingFailed = true;
					}
				}
				
				// distance wishes filter
				if(!checkingFailed) {
					if(this.distanceWishesArray.from != '' &&  this.distanceWishesArray.to != '') {
						var flag = false;
						for (var j=0; j<currentDriver.distancesArray.length;j++) {
							if(!(currentDriver.distancesArray[j].km_from > parseInt(this.distanceWishesArray.to) || currentDriver.distancesArray[j].km_to < parseInt(this.distanceWishesArray.from))) {
								flag = true;
							}
						}
						if(!flag) checkingFailed = true;
					}
					else if(this.distanceWishesArray.from != '' &&  this.distanceWishesArray.to == '') {
						var flag = false;
						for (var j=0; j<currentDriver.distancesArray.length;j++) {
							if(currentDriver.distancesArray[j].km_to >= parseInt(this.distanceWishesArray.from)) flag = true;
						}
						if(!flag) checkingFailed = true;
					}
					else if(this.distanceWishesArray.from == '' &&  this.distanceWishesArray.to != '') {
						var flag = false;
						for (var j=0; j<currentDriver.distancesArray.length;j++) {
							if(currentDriver.distancesArray[j].km_from <= parseInt(this.distanceWishesArray.to)) flag = true;
						}
						if(!flag) checkingFailed = true;
					}
				}

				// Если пройдены все проверки, то добавляем водителя к показываемым
				if(!checkingFailed) filteredDrivers.push(currentDriver);
			}
			
			this._filteredDrivers = filteredDrivers;
			var filteredDriversID = [];
			for(var i=0; i < this._filteredDrivers.length; i++) {
				filteredDriversID.push(this._filteredDrivers[i].ID_driver);
			}
			
			$(".driver-line").each(function() {
				if($.inArray($(this).attr('dr') + '', filteredDriversID) + 1) $(this).show();
				else $(this).hide();
			});
		},
	};

	Driver = function(driverArray) {
				this.carsArray = new Array(driverArray.Cars);
				this.ID_driver = driverArray.ID_driver;
				this.kmCost  = { from : driverArray.km_cost_from, to: driverArray.km_cost_to };
				this.lastExpirience = driverArray.last_experience;
				this.distancesArray = driverArray.distanceWishes;
	}